package com.example.demo.Interface;

import com.baomidou.mybatisplus.core.conditions.interfaces.Func;
import com.example.demo.Eneity.Employee;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 *
 * 四大核心函数接口
 *
 * Consumer<T>:消费行接口
 * Supplier<T>:供给型接口
 * Function
 * Predicate<T>
 *
 * @ClassName FourLambda
 * @Description TODO
 * Author yangqinglin
 * @Date 2021/1/16 12:44
 * @Version
 **/
public class FourLambda {


    @Test
    public void test1(){
        List<Integer> numList = getNumList(10, () -> (int)( Math.random() * 100));
        for (Integer i:
             numList) {
            System.out.println(i);
        }
    }





    public List<Integer>  getNumList(int num , Supplier<Integer> sp){
        List<Integer> list=new ArrayList<>();
        for (int i=0;i<num;i++){
            Integer n=sp.get();
            list.add(n);
        }
        return list;
    }


    @Test
    public void test2(){
        List<String> strs= Arrays.asList("nihao","haha","no","yang");

        List<String> strings = strHander(strs, (x) -> x.length() > 3);
        for (String s: strings
             ) {
            System.out.println(s);
        }
    }

//predicate
    public List<String>  strHander(List<String> strs, Predicate<String> pre){

        List<String>  lists=new ArrayList<>();
        for (String str: strs
             ) {

            if (pre.test(str)){
                lists.add(str);
            }

        }
        return lists;
    }


    @Test
    //方法引用
    public void test3(){

        Employee emp=new Employee();

        Supplier<Integer> sp=emp::getAge;
        sp.get();
        System.out.println(sp.get());

    }
    //类静态方法


    @Test
    public void test4(){


        //compare的参数和返回值类型一至
        Comparator<Integer> com=Integer::compare;
       //System.out.println(com.compare(1,2));
        BiPredicate<String ,String> bp=String::equals;
        //System.out.println( bp.test("abd","abc"));

        //构造器引用

        Supplier<Employee> sp1=()->new Employee();

        Supplier<Employee> sp2=Employee::new;


        Supplier<Employee> sp3=Employee::new;

        //单个字段应用返回对象
        Function<Integer,Employee> f1=(x)->new Employee(x);
        Employee apply = f1.apply(101);
//        System.out.println(apply);

        //简写法 f2.apply(102); 这里的参数要与构造参数列表一致
        Function<Integer,Employee> f2=Employee::new;
        Employee apply1 = f2.apply(102);
        //System.out.println(apply1);

        //数组引用

            //返回数组
        Function<Integer,String[]>  fun=(x)->new String[x];
        //System.out.println(fun.apply(10).length);

        Function<Integer,String[]> fun2=String[]::new;
        System.out.println(fun2.apply(20).length);





    }

}
